package com.example.SocialApplication.activity.lists.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.SocialApplication.HelperFactory;
import com.example.SocialApplication.R;
import com.example.SocialApplication.activity.dialog.ChooseIconThingDialog;
import com.example.SocialApplication.activity.dialog.ColorSpectrumDialog;
import com.example.SocialApplication.activity.dialog.ColorSpectrumInterface;
import com.example.SocialApplication.activity.dialog.IconThingInterface;
import com.example.SocialApplication.activity.lists.CreateListStatic;
import com.example.SocialApplication.db.entity.ColorEntity;
import com.example.SocialApplication.service.CallServiceGetThingImages;

import java.sql.SQLException;
import java.util.List;

public class DesignFragment extends Fragment implements ColorSpectrumInterface, IconThingInterface {
    private View root;
    private ColorSpectrumDialog dialog;
    private ChooseIconThingDialog mIconDialog;
    private List<String> mListIconNames;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.design_list_fragment, container, false);
        root = rootView;

        new CallServiceGetThingImages(){
            @Override
            protected void onPostExecute(List<String> strings) {
                mListIconNames = strings;
            }
        }.execute();

        final TextView text = (TextView) rootView.findViewById(R.id.name);

        Button button = (Button) rootView.findViewById(R.id.button);  ///todo slot for this button
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                    int size = HelperFactory.getHelper().getColorEntityDAO().queryForAll().size();
//                    Log.d("", String.valueOf(size));
                dialog = new ColorSpectrumDialog(getActivity(), DesignFragment.this);
                dialog.show();
            }
        });

        Button button2 = (Button) rootView.findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Toast.makeText(getActivity(), "NOT IMPLEMENTED !", Toast.LENGTH_SHORT).show();
                chooseIconListThings();
            }
        });

        EditText editText = (EditText) rootView.findViewById(R.id.editText);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                text.setText(editable);
                CreateListStatic.setToList().setName(editable.toString());
            }
        });
        return rootView;
    }

    @Override
    public void onSpectrumClick(ColorEntity entity, View view) {
        // todo save ColorEntity
        LinearLayout layout = (LinearLayout) root.findViewById(R.id.list_color_spectrum);
        layout.removeAllViews();

        LinearLayout childLayout = (LinearLayout) ((LinearLayout)view).getChildAt(0);
        ((ViewGroup)childLayout.getParent()).removeView(childLayout);

        layout.addView(childLayout);
        CreateListStatic.setToList().setColor(entity);
        dialog.dismiss();
    }

    @Override
    public void onIconCLick(String name) {
        CreateListStatic.setToList().setName(name);
        mIconDialog.dismiss();
        /// TODO set bitmap picture
    }

    private void chooseIconListThings() {
        mIconDialog = new ChooseIconThingDialog(getActivity(), mListIconNames, this);
        mIconDialog.show();
    }

    /// todo when save listThings in database, save it`s image to SD, and add field of it`s resource
}